﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Configuration;


public partial class DetExp : System.Web.UI.Page
{
    SqlConnection CNx = new SqlConnection();
    SqlConnection CNx2 = new SqlConnection();
    Utilitarios Util = new Utilitarios();
    string IDU, IDE;
    int Cta = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        CNx.ConnectionString = Utilitarios.Conexion2;
        CNx2.ConnectionString = Utilitarios.Conexion;
        if (Session["IDUsuario"] == null)
        {
            Response.Redirect("Default.aspx");
        };
        IDU = Session["IDUsuario"].ToString();
        IDE = Session["IDExpediente"].ToString(); 
        if (!IsPostBack)
        {
            cargardatos();
            Filtrar();
        }
        if (Page.Request.Params["__EVENTTARGET"] == "GrabarExp") GrabarExp();
        if (Page.Request.Params["__EVENTTARGET"] == "GrabarAct") GrabarAct();
    }

    private void cargardatos()
    {
        CNx.Open();
        hidIDExp.Value = IDE;
        string msg = "Select * from LEG_TipoProceso";
        SqlCommand tb = new SqlCommand(msg, CNx);
        SqlDataReader Dr = tb.ExecuteReader();
        ArrayList L1 = new ArrayList();

        while (Dr.Read())
        {
            L1.Add(new listas(Dr[0].ToString(), Dr[1].ToString()));
        }
        Dr.Close();
        cmbNtp.DataSource = L1;
        cmbNtp.DataTextField = "Nombre";
        cmbNtp.DataValueField = "Tag";
        cmbNtp.DataBind();

        msg = "Select * from LEG_VMateria";
        ArrayList L2 = new ArrayList();
        tb = new SqlCommand(msg, CNx);
        Dr = tb.ExecuteReader();
        while (Dr.Read())
        {
            L2.Add(new listas(Dr[0].ToString(), Dr[1].ToString()));
        }
        Dr.Close();
        cmbNmat.DataSource = L2;
        cmbNmat.DataTextField = "Nombre";
        cmbNmat.DataValueField = "Tag";
        cmbNmat.DataBind();

        msg = "Select * from LEG_Ubicacion";
        ArrayList L7 = new ArrayList();
        tb = new SqlCommand(msg, CNx);
        Dr = tb.ExecuteReader();
        while (Dr.Read())
        {
            L7.Add(new listas(Dr[0].ToString(), Dr[1].ToString()));
        }
        Dr.Close();
        cmbUbiFis.DataSource = L7;
        cmbUbiFis.DataTextField = "Nombre";
        cmbUbiFis.DataValueField = "Tag";
        cmbUbiFis.DataBind();

        cmbUbiFis2.DataSource = L7;
        cmbUbiFis2.DataTextField = "Nombre";
        cmbUbiFis2.DataValueField = "Tag";
        cmbUbiFis2.DataBind();

        CNx.Close();

        datosmateria();

        CNx2.Open();
        msg = "Select * from SIS_VUAPP where APP_ID=7 and SEG_1 >=256";
        ArrayList L3 = new ArrayList();
        tb = new SqlCommand(msg, CNx2);
        Dr = tb.ExecuteReader();
        while (Dr.Read())
        {
            L3.Add(new listas(Dr[0].ToString(), Dr[2].ToString()));
        }
        Dr.Close();
        cmbNabogado.DataSource = L3;
        cmbNabogado.DataTextField = "Nombre";
        cmbNabogado.DataValueField = "Tag";
        cmbNabogado.DataBind();
        CNx2.Close();


        CNx.Open();
        msg = "Select * from LEG_VActs where IDEx ="+IDE;
        tb = new SqlCommand(msg, CNx);
        Dr = tb.ExecuteReader();
        Dr.Read();
        lblNExp.Text = Dr["NroExped"].ToString();
        lblTipoPro.Text = Dr["NProceso"].ToString();
        lblMateria.Text = Dr["NMateria"].ToString();
        lblMotivo.Text = Dr["NMotivo"].ToString();
        lblJusgado.Text = Dr["NSala"].ToString();
        lblNJuzgado.Text = Dr["NomSala"].ToString();
        DateTime Fec = (DateTime)Dr["FechaExped"];
        lblFechaExp.Text = Fec.ToString("dd/MM/yyyy");
        lblJuez.Text = Dr["Juez"].ToString();
        lblSoles.Text = Dr["MontoS"].ToString();
        lblUS.Text = Dr["MontoUS"].ToString();
        string TDema = "Demandante";
        if ((Boolean)Dr["EstDem"])
        {
            TDema = "Demandado";
            rbtDado.Checked = true;
        }
        else rbtDante.Checked = true;
        lblDemanda.Text = TDema;
        lblNDemanda.Text = Dr["PDemanda"].ToString();
        int NAbo = (int)Dr["Abogado"];
        lblNUbicacion.Text = Dr["NUbicacion"].ToString()+" - "+Dr["DesUbica"].ToString();

        string IDMAT = Dr["IDMat"].ToString();
        txtNexp.Text = lblNExp.Text;
        cmbNtp.SelectedValue = Dr["IDTip"].ToString();
        cmbNmat.SelectedValue = IDMAT;
        cmbNmot.SelectedValue = Dr["IDSMat"].ToString();
        cmbNlocal.SelectedValue = Dr["IDLoc"].ToString();
        cmbUbiFis.SelectedValue = Dr["IDUbicacion"].ToString();
        cmbNabogado.SelectedValue = NAbo.ToString();


        txtNjuez.Text = lblJuez.Text;
        txtNsoles.Text = lblSoles.Text;
        txtNus.Text = lblUS.Text;
        txtNdemanda.Text = lblNDemanda.Text;
        txtUbiFis.Text = Dr["DesUbica"].ToString();
        Dr.Close();

        msg = "Select * from LEG_Estado where IDMat=" + IDMAT; ;
        ArrayList L8 = new ArrayList();
        tb = new SqlCommand(msg, CNx);
        Dr = tb.ExecuteReader();
        while (Dr.Read())
        {
            L8.Add(new listas(Dr[0].ToString(), Dr[2].ToString()));
        }
        Dr.Close();
        cmbEstado.DataSource = L8;
        cmbEstado.DataTextField = "Nombre";
        cmbEstado.DataValueField = "Tag";
        cmbEstado.DataBind();
        int Nanot = 0;
        msg = "Select count(*) from LEG_Anotaciones where IDExp = " + IDE;
        tb = new SqlCommand(msg, CNx);
        Nanot = (int)tb.ExecuteScalar();
        lblAnot.Text = Util.Ceros(Nanot, 2);
        CNx.Close(); 
        CNx2.Open();    
        msg = "Select Nombres from SIS_Usuarios where IDU =" + NAbo.ToString();
        SqlCommand tb2 = new SqlCommand(msg, CNx2);
        lblAbogado.Text = tb2.ExecuteScalar().ToString();
        CNx2.Close();

    }

    private void Filtrar()
    {
        CNx.Open();
        string msg = "Select IDAct, Fecha, Sumilla, PerRec, NEstado,EstVcmto, FechaVcmto from LEG_VActs20 where IDExp="+IDE;
        DataTable dt = new DataTable();
        SqlDataAdapter Da = new SqlDataAdapter(msg, CNx);
        Da.Fill(dt);
        gvListado.DataSource = dt;
        gvListado.DataBind();
        CNx.Close();
    }


    protected void gvListado_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            DateTime F1 = (DateTime)rowView["Fecha"];
            string IDCon = rowView["IDAct"].ToString();
            Boolean d1 = (bool)rowView["EstVcmto"];
            int Uper = (int)rowView["PerRec"];
            ImageButton btn1 = (ImageButton)e.Row.FindControl("imbDcmto");
            ImageButton btn5 = (ImageButton)e.Row.FindControl("CimBtnDelete");
            ImageButton btn2 = (ImageButton)e.Row.FindControl("CimBtnEdit");
            string RutaArch;
            Cta++;
            e.Row.Cells[2].Text = Cta.ToString();
            if (d1) e.Row.Cells[7].Text = F1.ToString("dd/MM/yyyy");
            string UU = Util.UserNombre(Uper);
            e.Row.Cells[5].Text = UU;
            RutaArch = "documentos/Actuados/A" + Util.Ceros(int.Parse(IDCon), 6) + ".pdf";
            btn1.Attributes.Add("OnClick", "return verpdf('" + RutaArch + "','Comprobante de pago');");
            btn2.Attributes.Add("OnClick", "return editaract(" + IDCon + ");");
            if (Cta == 1) btn5.Visible = false;


        }
    }

    private void datosmateria()
    {
        string IDM = cmbNmat.SelectedValue;
        if (CNx.State == ConnectionState.Closed) CNx.Open();
        string msg = "Select * from LEG_SMateria where IDM=" + IDM;
        SqlCommand tb = new SqlCommand(msg, CNx);
        SqlDataReader Dr = tb.ExecuteReader();
        ArrayList L1 = new ArrayList();
        while (Dr.Read())
        {
            L1.Add(new listas(Dr[0].ToString(), Dr[2].ToString()));
        }
        Dr.Close();
        cmbNmot.DataSource = L1;
        cmbNmot.DataTextField = "Nombre";
        cmbNmot.DataValueField = "Tag";
        cmbNmot.DataBind();
        Dr.Close();
        msg = "Select NSala from LEG_VMateria where IDM=" + IDM;
        tb = new SqlCommand(msg, CNx);
        lblNlocal.Text = tb.ExecuteScalar().ToString();
        msg = "Select * from LEG_Sala where IDM=" + IDM;
        tb = new SqlCommand(msg, CNx);
        Dr = tb.ExecuteReader();
        ArrayList L2 = new ArrayList();
        while (Dr.Read())
        {
            L2.Add(new listas(Dr[0].ToString(), Dr[2].ToString()));
        }
        Dr.Close();
        cmbNlocal.DataSource = L2;
        cmbNlocal.DataTextField = "Nombre";
        cmbNlocal.DataValueField = "Tag";
        cmbNlocal.DataBind();
        Dr.Close();
        CNx.Close();
    }
    protected void cmbNmat_SelectedIndexChanged(object sender, EventArgs e)
    {
        datosmateria();
    }

    private void GrabarExp()
    {
        if (txtNsoles.Text == "") txtNsoles.Text = "0.00";
        if (txtNus.Text == "") txtNus.Text = "0.00";
                 string msg = "";
                 if (hidFlag.Value == "1")
                 {
                     string EDem = "0";

                     if (rbtDado.Checked == true) EDem = "1";

                     msg = "LEG_EExpe " + IDU + "," + IDE + ",'" + txtNexp.Text + "'," + cmbNtp.SelectedValue + "," + cmbNmat.SelectedValue + "," + cmbNmot.SelectedValue + "," + cmbNlocal.SelectedValue;
                     msg = msg +","+ EDem + ",'" + txtNdemanda.Text + "','" + txtNjuez.Text + "'," + cmbNabogado.SelectedValue + ",";
                     msg = msg + txtNsoles.Text + "," + txtNus.Text + "," + cmbUbiFis.SelectedValue + ",'" + txtUbiFis.Text + "'";

                     CNx.Open();
                     SqlCommand tb = new SqlCommand(msg, CNx);
                     tb.ExecuteNonQuery();

                     CNx.Close();
                     cargardatos();
                 }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        editaractuado();
    }

    private void editaractuado()
    {
        string id = hidConf.Value;
        string msg = "Select * from LEG_Actuados where IDact = " + id;
        CNx.Open();
        SqlCommand tb = new SqlCommand(msg, CNx);
        SqlDataReader Dr = tb.ExecuteReader();
        Dr.Read();
        DateTime FF = (DateTime)Dr["Fecha"];
        txtNfecha.Text = FF.ToString("dd/MM/yyyy");
        FF = (DateTime)Dr["FechaVcmto"];
        txtVfecha.Text = FF.ToString("dd/MM/yyyy");
        if ((Boolean)Dr["EstVcmto"]) chkVfecha.Checked = true;
        else
        {
            chkVfecha.Checked = false;
            txtVfecha.Attributes.Add("Style","display: none");
        }
        txtNsumilla.Text = Dr["Sumilla"].ToString();
        cmbEstado.SelectedValue = Dr["Estado"].ToString();
        cmbUbiFis2.SelectedValue = Dr["idUbicacion"].ToString();
        txtUbiFis2.Text = Dr["DesUbica"].ToString();
        Dr.Close();
        CNx.Close();

    }

    private void GrabarAct()
    {
        string msg, estV="false";
        string idAc = hidConf.Value;
        if (chkVfecha.Checked) estV = "true";
        if (idAc == "0")
        {
            msg = "LEG_NAct " + hidIDExp.Value + ",@F1,@F2," + estV + ",'" + txtNsumilla.Text  + "'," + cmbEstado.SelectedValue + "," + cmbUbiFis2.SelectedValue + ",'" + txtUbiFis2.Text + "',"+IDU;
        }
        else
        {
            msg = "LEG_EAct " + idAc + ",@F1,@F2," + estV + ",'" + txtNsumilla.Text  + "'," + cmbEstado.SelectedValue + "," + cmbUbiFis2.SelectedValue + ",'" + txtUbiFis2.Text +"'," + IDU; ;
        }


        string ftt = txtVfecha.Text;
        if (ftt == "") ftt = txtNfecha.Text;
        ftt = Util.Afecha(ftt);
        DateTime F1 = DateTime.Parse(ftt);

        ftt = txtNfecha.Text;
        ftt = Util.Afecha(ftt);
        DateTime F2 = DateTime.Parse(ftt);
        CNx.Open();
        SqlCommand tb = new SqlCommand(msg, CNx);
        tb.Parameters.Add("@F1", SqlDbType.DateTime).Value = F1;
        tb.Parameters.Add("@F2", SqlDbType.DateTime).Value = F2;
        int idActu = (int)tb.ExecuteScalar();

        if (FUExpediente.FileName != "")
        {
            string archi = "Documentos/Actuados/A" + Util.Ceros(idActu, 6) + ".pdf";
            FUExpediente.SaveAs(MapPath("~/" + archi));
        }

        CNx.Close();
        cargardatos();
        Filtrar();
    }

    protected void gvListado_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = gvListado.DataKeys[e.RowIndex].Value.ToString();
        string msg = "LEG_BAct " + id+","+IDU;
        CNx.Open();
        SqlCommand tb = new SqlCommand(msg, CNx);
        tb.ExecuteNonQuery();
        CNx.Close();
        cargardatos();
        Filtrar();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string msg="Select * from LEG_Logs where IDExp = "+IDE+" order by Fecha Desc"; // 
        CNx.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter Da = new SqlDataAdapter(msg, CNx);
        Da.Fill(dt);
        gvLogs.DataSource = dt;
        gvLogs.DataBind();
        CNx.Close();

    }
    protected void gvLogs_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            int Uper = int.Parse(e.Row.Cells[1].Text);
            string UU = Util.UserNombre(Uper);
            e.Row.Cells[1].Text = UU;

        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string msg = "Select * from LEG_Anotaciones where IDExp = " + IDE + " order by Fecha Desc";
        CNx.Open();
        SqlCommand tb = new SqlCommand(msg, CNx);
        SqlDataReader Dr = tb.ExecuteReader();
        while (Dr.Read())
          {
             HyperLink IpB = new HyperLink();
             IpB.CssClass = "lista_grupo";
             DateTime FF = (DateTime)Dr["Fecha"];
             int UuI = (int)Dr["PerRec"];
             string Texto = Util.UserNombres(UuI) +" ["+ FF.ToString("dd/MM/yy HH:mm")+"]";
             string Dato = Dr["Anotacion"].ToString();
             string txtdato = "<h4 class='list-group-item-heading' style='line-height:15px;'>" + Texto + "</h4>";
             txtdato = txtdato + "<p class='list-group-item-text' style='Font-size:12px;line-height:15px;'>" + Dato + "</p>";
             IpB.Text = txtdato;
             areadatos.Controls.Add(IpB);
          }
        Dr.Close();
        CNx.Close();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        CNx.Open();
        string msg = "Insert into LEG_Anotaciones (IDexp,PerRec,Anotacion) values (" + IDE + "," + IDU + ",'" + txtNAnot.Text + "')";
        SqlCommand tb = new SqlCommand(msg, CNx);
        tb.ExecuteNonQuery();
        CNx.Close();
    }
    protected void gvListado_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvListado.PageIndex = e.NewPageIndex;
        Filtrar();
    }
}